1 /*
2 Copyright (c)
2014 Andrew Jones, Dario Seyb
3  Based
on 'Spriter2Unity' python code by Malhavok
4
5 Permission
is hereby granted, free of charge, to any person obtaining a copy
6 of
this software and associated documentation files (the "Software"), to deal
7 in
the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software
is
10 furnished to
do so, subject to the following conditions:
11
12 The above copyright notice and
this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
22 */

23
24 using
System;
25 using
System.Collections.Generic;
26 using
System.Linq;
27 using
System.Text;
28 using
UnityEngine;
29 using
UnityEditor;
30 using
Assets.ThirdParty.Spriter2Unity.Editor.Spriter;
31 using
Spriter2Unity.Runtime;
32
33 namespace
Assets.ThirdParty.Spriter2Unity.Editor.Unity
34 {
35     
public class AnimationCurveBuilder
36     {
37         
private class ObjectCurves
38         {
39             
public bool IsSpriteKey;
40             
public AnimationCurve[] Curves;
41         }
42
43         Dictionary<
string, ObjectCurves> curveCache = new Dictionary<string, ObjectCurves>();
44
45         
public void AddCurves(AnimationClip animClip)
46         {
47             
foreach(var kvp in curveCache)
48             {
49                 
var curves = kvp.Value.Curves;
50                 
//Position curves
51                 SetCurveIfNotEmpty(
ref animClip, kvp.Key, typeof(Transform), "localPosition.x", curves[(int)AnimationCurveIndex.LocalPositionX]);
52                 SetCurveIfNotEmpty(
ref animClip, kvp.Key, typeof(Transform), "localPosition.y", curves[(int)AnimationCurveIndex.LocalPositionY]);
53                 SetCurveIfNotEmpty(
ref animClip, kvp.Key, typeof(Transform), "localPosition.z", curves[(int)AnimationCurveIndex.LocalPositionZ]);
54
55                 
//Rotation curves
56                 SetCurveIfNotEmpty(
ref animClip, kvp.Key, typeof(Transform), "localRotation.x", curves[(int)AnimationCurveIndex.LocalRotationX]);
57                 SetCurveIfNotEmpty(
ref animClip, kvp.Key, typeof(Transform), "localRotation.y", curves[(int)AnimationCurveIndex.LocalRotationY]);
58                 SetCurveIfNotEmpty(
ref animClip, kvp.Key, typeof(Transform), "localRotation.z", curves[(int)AnimationCurveIndex.LocalRotationZ]);
59                 SetCurveIfNotEmpty(
ref animClip, kvp.Key, typeof(Transform), "localRotation.w", curves[(int)AnimationCurveIndex.LocalRotationW]);
60
61                 
//Scale curves
62                 SetCurveIfNotEmpty(
ref animClip, kvp.Key, typeof(Transform), "localScale.x", curves[(int)AnimationCurveIndex.LocalScaleX]);
63                 SetCurveIfNotEmpty(
ref animClip, kvp.Key, typeof(Transform), "localScale.y", curves[(int)AnimationCurveIndex.LocalScaleY]);
64                 SetCurveIfNotEmpty(
ref animClip, kvp.Key, typeof(Transform), "localScale.z", curves[(int)AnimationCurveIndex.LocalScaleZ]);
65
66                 
//IsActive curve
67                 SetCurveIfNotEmpty(
ref animClip, kvp.Key, typeof(GameObject), "m_IsActive", curves[(int)AnimationCurveIndex.IsActive]);
68
69                 
if (kvp.Value.IsSpriteKey)
70                 {
71                     
//Color Tint curve
72                     SetCurveIfNotEmpty(
ref animClip, kvp.Key, typeof(SpriteRenderer), "m_Color.r", curves[(int)AnimationCurveIndex.ColorR]);
73                     SetCurveIfNotEmpty(
ref animClip, kvp.Key, typeof(SpriteRenderer), "m_Color.g", curves[(int)AnimationCurveIndex.ColorG]);
74                     SetCurveIfNotEmpty(
ref animClip, kvp.Key, typeof(SpriteRenderer), "m_Color.b", curves[(int)AnimationCurveIndex.ColorB]);
75                     SetCurveIfNotEmpty(
ref animClip, kvp.Key, typeof(SpriteRenderer), "m_Color.a", curves[(int)AnimationCurveIndex.ColorA]);
76                     SetCurveIfNotEmpty(
ref animClip, kvp.Key, typeof(SortingOrderUpdate), "SortingOrder", curves[(int)AnimationCurveIndex.ZIndex]);
77                 }
78             }
79
80             
//animClip.EnsureQuaternionContinuity();
81         }
82
83         
public void SetCurveRecursive(Transform root, float time)
84         {
85             SetCurveRecursive(root, root, time);
86         }
87
88         
private void SetCurveRecursive(Transform root, Transform current, float time)
89         {
90             
if(root != current)
91                 SetCurve(root, current, time);
92             
foreach(Transform child in current.transform)
93             {
94                 SetCurveRecursive(root, child, time);
95             }
96         }
97
98         
public void SetCurve(Transform root, Transform current, float time)
99         {
100             SetCurve(root, current, time,
null);
101         }
102
103         
public void SetCurve(Transform root, Transform current, float time, TimelineKey lastTimelineKey, int zIndex = -1)
104         {
105             
var path = AnimationUtility.CalculateTransformPath(current, root);
106             
var curves = GetOrCreateAnimationCurves(path);
107             UpdateTransformCurve(curves, current, time, lastTimelineKey, zIndex);
108         }
109
110         
public void SetCurveActiveOnly(Transform root, Transform current, float time)
111         {
112             
var path = AnimationUtility.CalculateTransformPath(current, root);
113             
var obj = GetOrCreateAnimationCurves(path);
114
115             
//IsActive curve
116             
float val = (current.gameObject.activeInHierarchy) ? 1.0f : 0.0f;
117             obj.Curves[(
int)AnimationCurveIndex.IsActive].AddKey(new Keyframe(time, val, float.PositiveInfinity, float.PositiveInfinity) { tangentMode = 0 });
118         }
119
120         
private void SetCurveIfNotEmpty(ref AnimationClip clip, string path, Type component, string property, AnimationCurve curve)
121         {
122             
if (curve.keys.Length > 0)
123             {
124                 clip.SetCurve(path, component, property, curve);
125             }
126         }
127
128         
private void UpdateTransformCurve(ObjectCurves obj, Transform current, float time, TimelineKey lastTimelineKey, int zIndex = -1)
129         {
130             
float val;
131             
//IsActive curve
132             val = (current.gameObject.activeSelf) ?
1.0f : 0.0f;
133             obj.Curves[(
int)AnimationCurveIndex.IsActive].AddKey(new Keyframe(time, val, float.PositiveInfinity, float.PositiveInfinity) { tangentMode = 0 });
134
135             
//Position curves
136             obj.Curves[(
int)AnimationCurveIndex.LocalPositionX].AddKey(new Keyframe(time, current.localPosition.x) { tangentMode = 0 }, lastTimelineKey);
137             obj.Curves[(
int)AnimationCurveIndex.LocalPositionY].AddKey(new Keyframe(time, current.localPosition.y) { tangentMode = 0 }, lastTimelineKey);
138             obj.Curves[(
int)AnimationCurveIndex.LocalPositionZ].AddKey(new Keyframe(time, current.localPosition.z, float.PositiveInfinity, float.PositiveInfinity)); //Z value always has instant transition
139
140             
//Rotation curves
141             
var quat = Quaternion.Euler(current.localEulerAngles);
142             obj.Curves[(
int)AnimationCurveIndex.LocalRotationX].AddKey(new Keyframe(time, quat.x) { tangentMode = 0 }, lastTimelineKey);
143             obj.Curves[(
int)AnimationCurveIndex.LocalRotationY].AddKey(new Keyframe(time, quat.y) { tangentMode = 0 }, lastTimelineKey);
144             obj.Curves[(
int)AnimationCurveIndex.LocalRotationZ].AddKey(new Keyframe(time, quat.z) { tangentMode = 0 }, lastTimelineKey);
145             obj.Curves[(
int)AnimationCurveIndex.LocalRotationW].AddKey(new Keyframe(time, quat.w) { tangentMode = 0 }, lastTimelineKey);
146
147             
//Scale curves
148             obj.Curves[(
int)AnimationCurveIndex.LocalScaleX].AddKey(new Keyframe(time, current.localScale.x) { tangentMode = 0 }, lastTimelineKey);
149             obj.Curves[(
int)AnimationCurveIndex.LocalScaleY].AddKey(new Keyframe(time, current.localScale.y) { tangentMode = 0 }, lastTimelineKey);
150             obj.Curves[(
int)AnimationCurveIndex.LocalScaleZ].AddKey(new Keyframe(time, current.localScale.z) { tangentMode = 0 }, lastTimelineKey);
151
152             
//Sprite Curves
153             
var spriteTimelineKey = lastTimelineKey as SpriteTimelineKey;
154             
if (spriteTimelineKey != null)
155             {
156                 obj.IsSpriteKey =
true;
157                 obj.Curves[(
int)AnimationCurveIndex.ColorR].AddKey(new Keyframe(time, spriteTimelineKey.Tint.r) { tangentMode = 0 }, lastTimelineKey);
158                 obj.Curves[(
int)AnimationCurveIndex.ColorG].AddKey(new Keyframe(time, spriteTimelineKey.Tint.g) { tangentMode = 0 }, lastTimelineKey);
159                 obj.Curves[(
int)AnimationCurveIndex.ColorB].AddKey(new Keyframe(time, spriteTimelineKey.Tint.b) { tangentMode = 0 }, lastTimelineKey);
160                 obj.Curves[(
int)AnimationCurveIndex.ColorA].AddKey(new Keyframe(time, spriteTimelineKey.Tint.a) { tangentMode = 0 }, lastTimelineKey);
161                 obj.Curves[(
int)AnimationCurveIndex.ZIndex].AddKey(new Keyframe(time, zIndex, float.PositiveInfinity, float.PositiveInfinity));
162             }
163         }
164
165         
private ObjectCurves GetOrCreateAnimationCurves(string path)
166         {
167             ObjectCurves objCurves;
168
169             
if (!curveCache.TryGetValue(path, out objCurves))
170             {
171                 objCurves =
new ObjectCurves();
172                 objCurves.Curves =
new AnimationCurve[(int)AnimationCurveIndex.ENUM_COUNT];
173                 
for (int i = 0; i < (int) AnimationCurveIndex.ENUM_COUNT; i++)
174                 {
175                     objCurves.Curves[i] =
new AnimationCurve();
176                 }
177                 curveCache[path] = objCurves;
178             }
179             
return objCurves;
180         }
181
182         
private enum AnimationCurveIndex
183         {
184             LocalPositionX,
185             LocalPositionY,
186             LocalPositionZ,
187             LocalRotationX,
188             LocalRotationY,
189             LocalRotationZ,
190             LocalRotationW,
191             LocalScaleX,
192             LocalScaleY,
193             LocalScaleZ,
194             IsActive,
195             ColorR,
196             ColorG,
197             ColorB,
198             ColorA,
199             ZIndex,
200             ENUM_COUNT,
201         }
202     }
203 }


Position curves

Rotation curves

Scale curves

IsActive curve

Color Tint curve

animClip.EnsureQuaternionContinuity();

IsActive curve

IsActive curve

Position curves

obj.Curves[(int)AnimationCurveIndex.LocalPositionZ].AddKey(new Keyframe(time, current.localPosition.z, float.PositiveInfinity, float.PositiveInfinity)); Z value always has instant transition

Rotation curves

Scale curves

Sprite Curves




Trò chơi đua xe động vật trong UNITY Engine 114.947 lượt xem

Gõ tìm kiếm nhanh...